<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="waName">
        <el-input
          v-model="queryParams.waName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="jobNum">
        <el-input
          v-model="queryParams.jobNum"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >同步</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['hr:payment:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['hr:payment:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paymentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司代码" align="center" prop="factoryCode" />
      <el-table-column label="公司名称" align="center" prop="factoryName" />
      <el-table-column label="姓名" align="center" prop="waName" />
      <el-table-column label="工号" align="center" prop="jobNum" />
      <el-table-column label="部门" align="center" prop="department" />
      <el-table-column label="分部" align="center" prop="division" />
      <el-table-column label="组别" align="center" prop="waGroup" />
      <el-table-column label="岗位" align="center" prop="post" />
      <el-table-column label="当前年月" align="center" prop="currentYm" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.currentYm, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入职年月" align="center" prop="entryYm" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryYm, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保险情况" align="center" prop="insurSituation" />
      <el-table-column label="入职分类" align="center" prop="entryClassification" />
      <el-table-column label="是否转正" align="center" prop="isFormal" />
      <el-table-column label="应发基本工资" align="center" prop="yfBasicWages" />
      <el-table-column label="应发岗位责任工资" align="center" prop="yfGwzrWages" />
      <el-table-column label="应发加班工资" align="center" prop="yfJbWages" />
      <el-table-column label="应发计件工资" align="center" prop="yfJjWages" />
      <el-table-column label="应发交通补贴" align="center" prop="yfJtWages" />
      <el-table-column label="应发通讯补贴" align="center" prop="yfTxWages" />
      <el-table-column label="全勤奖" align="center" prop="qqj" />
      <el-table-column label="学历补贴" align="center" prop="xlbt" />
      <el-table-column label="星级奖励" align="center" prop="xjjl" />
      <el-table-column label="其他补贴" align="center" prop="otherBt" />
      <el-table-column label="高温/取暖补贴" align="center" prop="gwbt" />
      <el-table-column label="特殊补贴" align="center" prop="specialBt" />
      <el-table-column label="餐补" align="center" prop="cb" />
      <el-table-column label="应发工资合计" align="center" prop="yfWagesTotal" />
      <el-table-column label="保险" align="center" prop="insurance" />
      <el-table-column label="住房公积金" align="center" prop="zfgjj" />
      <el-table-column label="个税" align="center" prop="grsds" />
      <el-table-column label="工作服等扣款" align="center" prop="gzfkk" />
      <el-table-column label="其他扣款" align="center" prop="otherKk" />
      <el-table-column label="考核罚没款" align="center" prop="khfmj" />
      <el-table-column label="奖励工资" align="center" prop="reward" />
      <el-table-column label="实发工资合计" align="center" prop="actualWagesTotal" />
      <el-table-column label="是否发放" align="center" prop="idPay" />
      <el-table-column label="原因" align="center" prop="reason" />
      <el-table-column label="是否归档" align="center" prop="isArchive" />
      <el-table-column label="签字" align="center" prop="sign" />
      <el-table-column label="工资卡号" align="center" prop="waCardNum" />
      <el-table-column label="开户银行" align="center" prop="openBank" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['hr:payment:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['hr:payment:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工资发放总对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司代码" prop="factoryCode">
          <el-input v-model="form.factoryCode" placeholder="请输入公司代码" />
        </el-form-item>
        <el-form-item label="公司名称" prop="factoryName">
          <el-input v-model="form.factoryName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="姓名" prop="waName">
          <el-input v-model="form.waName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="工号" prop="jobNum">
          <el-input v-model="form.jobNum" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-input v-model="form.department" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="分部" prop="division">
          <el-input v-model="form.division" placeholder="请输入分部" />
        </el-form-item>
        <el-form-item label="组别" prop="waGroup">
          <el-input v-model="form.waGroup" placeholder="请输入组别" />
        </el-form-item>
        <el-form-item label="岗位" prop="post">
          <el-input v-model="form.post" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item label="当前年月" prop="currentYm">
          <el-date-picker clearable
            v-model="form.currentYm"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择当前年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入职年月" prop="entryYm">
          <el-date-picker clearable
            v-model="form.entryYm"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入职年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保险情况" prop="insurSituation">
          <el-input v-model="form.insurSituation" placeholder="请输入保险情况" />
        </el-form-item>
        <el-form-item label="入职分类" prop="entryClassification">
          <el-input v-model="form.entryClassification" placeholder="请输入入职分类" />
        </el-form-item>
        <el-form-item label="是否转正" prop="isFormal">
          <el-input v-model="form.isFormal" placeholder="请输入是否转正" />
        </el-form-item>
        <el-form-item label="应发基本工资" prop="yfBasicWages">
          <el-input v-model="form.yfBasicWages" placeholder="请输入应发基本工资" />
        </el-form-item>
        <el-form-item label="应发岗位责任工资" prop="yfGwzrWages">
          <el-input v-model="form.yfGwzrWages" placeholder="请输入应发岗位责任工资" />
        </el-form-item>
        <el-form-item label="应发加班工资" prop="yfJbWages">
          <el-input v-model="form.yfJbWages" placeholder="请输入应发加班工资" />
        </el-form-item>
        <el-form-item label="应发计件工资" prop="yfJjWages">
          <el-input v-model="form.yfJjWages" placeholder="请输入应发计件工资" />
        </el-form-item>
        <el-form-item label="应发交通补贴" prop="yfJtWages">
          <el-input v-model="form.yfJtWages" placeholder="请输入应发交通补贴" />
        </el-form-item>
        <el-form-item label="应发通讯补贴" prop="yfTxWages">
          <el-input v-model="form.yfTxWages" placeholder="请输入应发通讯补贴" />
        </el-form-item>
        <el-form-item label="全勤奖" prop="qqj">
          <el-input v-model="form.qqj" placeholder="请输入全勤奖" />
        </el-form-item>
        <el-form-item label="学历补贴" prop="xlbt">
          <el-input v-model="form.xlbt" placeholder="请输入学历补贴" />
        </el-form-item>
        <el-form-item label="星级奖励" prop="xjjl">
          <el-input v-model="form.xjjl" placeholder="请输入星级奖励" />
        </el-form-item>
        <el-form-item label="其他补贴" prop="otherBt">
          <el-input v-model="form.otherBt" placeholder="请输入其他补贴" />
        </el-form-item>
        <el-form-item label="高温/取暖补贴" prop="gwbt">
          <el-input v-model="form.gwbt" placeholder="请输入高温/取暖补贴" />
        </el-form-item>
        <el-form-item label="特殊补贴" prop="specialBt">
          <el-input v-model="form.specialBt" placeholder="请输入特殊补贴" />
        </el-form-item>
        <el-form-item label="餐补" prop="cb">
          <el-input v-model="form.cb" placeholder="请输入餐补" />
        </el-form-item>
        <el-form-item label="应发工资合计" prop="yfWagesTotal">
          <el-input v-model="form.yfWagesTotal" placeholder="请输入应发工资合计" />
        </el-form-item>
        <el-form-item label="保险" prop="insurance">
          <el-input v-model="form.insurance" placeholder="请输入保险" />
        </el-form-item>
        <el-form-item label="住房公积金" prop="zfgjj">
          <el-input v-model="form.zfgjj" placeholder="请输入住房公积金" />
        </el-form-item>
        <el-form-item label="个税" prop="grsds">
          <el-input v-model="form.grsds" placeholder="请输入个税" />
        </el-form-item>
        <el-form-item label="工作服等扣款" prop="gzfkk">
          <el-input v-model="form.gzfkk" placeholder="请输入工作服等扣款" />
        </el-form-item>
        <el-form-item label="其他扣款" prop="otherKk">
          <el-input v-model="form.otherKk" placeholder="请输入其他扣款" />
        </el-form-item>
        <el-form-item label="考核罚没款" prop="khfmj">
          <el-input v-model="form.khfmj" placeholder="请输入考核罚没款" />
        </el-form-item>
        <el-form-item label="奖励工资" prop="reward">
          <el-input v-model="form.reward" placeholder="请输入奖励工资" />
        </el-form-item>
        <el-form-item label="实发工资合计" prop="actualWagesTotal">
          <el-input v-model="form.actualWagesTotal" placeholder="请输入实发工资合计" />
        </el-form-item>
        <el-form-item label="是否发放" prop="idPay">
          <el-input v-model="form.idPay" placeholder="请输入是否发放" />
        </el-form-item>
        <el-form-item label="原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入原因" />
        </el-form-item>
        <el-form-item label="是否归档" prop="isArchive">
          <el-input v-model="form.isArchive" placeholder="请输入是否归档" />
        </el-form-item>
        <el-form-item label="签字" prop="sign">
          <el-input v-model="form.sign" placeholder="请输入签字" />
        </el-form-item>
        <el-form-item label="工资卡号" prop="waCardNum">
          <el-input v-model="form.waCardNum" placeholder="请输入工资卡号" />
        </el-form-item>
        <el-form-item label="开户银行" prop="openBank">
          <el-input v-model="form.openBank" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayment, getPayment, delPayment, addPayment, updatePayment ,tbGzhs} from "@/api/hr/payment";
import {getToken} from "@/utils/auth";

export default {
  name: "Payment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工资发放总表格数据
      paymentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryCode: null,
        factoryName: null,
        waName: null,
        jobNum: null,
        department: null,
        division: null,
        waGroup: null,
        post: null,
        currentYm: null,
        entryYm: null,
        insurSituation: null,
        entryClassification: null,
        isFormal: null,
        yfBasicWages: null,
        yfGwzrWages: null,
        yfJbWages: null,
        yfJjWages: null,
        yfJtWages: null,
        yfTxWages: null,
        qqj: null,
        xlbt: null,
        xjjl: null,
        otherBt: null,
        gwbt: null,
        specialBt: null,
        cb: null,
        yfWagesTotal: null,
        insurance: null,
        zfgjj: null,
        grsds: null,
        gzfkk: null,
        otherKk: null,
        khfmj: null,
        reward: null,
        actualWagesTotal: null,
        idPay: null,
        reason: null,
        isArchive: null,
        sign: null,
        waCardNum: null,
        openBank: null,
        idCard: null
      },
      // 表单参数
      form: {},
      //考勤导入
      upload: {
        // 是否显示弹出层（物料导入）
        open: false,
        // 弹出层标题（物料导入）
        title: "导入数据",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/hr/payment/importData"
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('hr/payment/importTemplate', {
      }, `工资表.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 查询工资发放总列表 */
    getList() {
      this.loading = true;
      listPayment(this.queryParams).then(response => {
        this.paymentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryCode: null,
        factoryName: null,
        waName: null,
        jobNum: null,
        department: null,
        division: null,
        waGroup: null,
        post: null,
        currentYm: null,
        entryYm: null,
        insurSituation: null,
        entryClassification: null,
        isFormal: null,
        yfBasicWages: null,
        yfGwzrWages: null,
        yfJbWages: null,
        yfJjWages: null,
        yfJtWages: null,
        yfTxWages: null,
        qqj: null,
        xlbt: null,
        xjjl: null,
        otherBt: null,
        gwbt: null,
        specialBt: null,
        cb: null,
        yfWagesTotal: null,
        insurance: null,
        zfgjj: null,
        grsds: null,
        gzfkk: null,
        otherKk: null,
        khfmj: null,
        reward: null,
        actualWagesTotal: null,
        idPay: null,
        reason: null,
        isArchive: null,
        sign: null,
        waCardNum: null,
        openBank: null,
        idCard: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      tbGzhs().then(response => {
        if(response.code==200){
          this.getList();
          this.$modal.msgSuccess("同步成功");
        }
        loading.close();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPayment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工资发放总";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePayment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工资发放总编号为"' + ids + '"的数据项？').then(function() {
        return delPayment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hr/payment/export', {
        ...this.queryParams
      }, `payment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
